package com.hmall.search.web;

import com.hmall.common.basecontext.BaseContext;
import com.hmall.common.dto.PageDTO;
import com.hmall.search.pojo.ItemDoc;
import com.hmall.search.pojo.RequestParams;
import com.hmall.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 条件查询索引库信息
     * @param requestParams
     * @return
     */
    /*Request URL: http://localhost:10010/search/list
    Request Method: POST
    {"key":"","page":1,"size":20,"sortBy":"default"}*/
    @PostMapping("/list")
    public PageDTO list(@RequestBody RequestParams requestParams){
        log.info("[商品搜索功能分页查询===>前端数据：{}]",requestParams.toString());
        log.info("[商品搜索功能分页查询===>ThreadLocal中参数输出测试：{}]",BaseContext.getCurrentId());
        return searchService.search(requestParams);
    }

    /**
     * 过滤项聚合
     * @param requestParams
     * @return
     */
    /*Request URL: http://localhost:10010/search/filters
    Request Method: POST
    {"key":"","page":1,"size":20,"sortBy":"default"}*/
    @PostMapping("/filters")
    public Map<String, List<String>> filters(@RequestBody RequestParams requestParams){
        log.info("[商品搜索功能过滤项聚合===>前端数据：{}]",requestParams.toString());
        return searchService.getFilters(requestParams);
    }

}
